package effectiveJava.myEnum;

/**
 * Created by dycong on 2017/9/4.
 * todo 更好的方法，可以采用策略枚举的方式：每当添加一个枚举常量时，强制选择一种加班报酬策略
 */
enum PayrollDay3 {
    MONDAY(PayType.WEEKDAY), TUESDAY(PayType.WEEKDAY), WEDNESDAY(
            PayType.WEEKDAY), THURSDAY(PayType.WEEKDAY), FRIDAY(PayType.WEEKDAY), SATURDAY(
            PayType.WEEKEND), SUNDAY(PayType.WEEKEND);

    private final PayType payType;

    PayrollDay3(PayType payType) {
        this.payType = payType;
    }

    public double pay(double hoursWorked, double payRate) {
        return payType.pay(hoursWorked, payRate);
    }

    // The strategy enum type
    private enum PayType {
        WEEKDAY {
            double overtimePay(double hours, double payRate) {
                return hours <= HOURS_PER_SHIFT ? 0 : (hours - HOURS_PER_SHIFT)
                        * payRate / 2;
            }
        },
        WEEKEND {
            double overtimePay(double hours, double payRate) {
                return hours * payRate / 2;
            }
        };
        private static final int HOURS_PER_SHIFT = 8;

        abstract double overtimePay(double hrs, double payRate);

        double pay(double hoursWorked, double payRate) {
            double base = hoursWorked * payRate;
            /*todo 调用抽象方法overtimePay()，实际中会调用具体子类的方法，
            todo 骨架类的设计原则：将基础方法设为抽象，将以基础方法为基础实现的方法具体实现，调用抽象方法*/
            return base + overtimePay(hoursWorked, payRate);
        }

    }
}
